<?php
add_action('widgets_init', 'register_footer_links_widget');

function register_footer_links_widget() {
    register_widget('footer_links');
}

class footer_links extends WP_Widget // widget class 
{
    function footer_links() { // widget setting | class name must be same function name
        $widget_ops = array('classname' => 'Footer-links-w', 'description' =>
            'Display inner links at footer');
        $control_ops = array(
            'width' => 250,
            'height' => 350,
            'id_base' => 'footer_links');
        $this->WP_Widget('footer_links', 'CouponX - Footer links', $widget_ops, $control_ops);
    }
    
    function widget($args, $instance) { // display widget 
        extract($args);
        $cl1 = $instance['slPages_cl1'];
        $cl2 = $instance['slPages_cl2'];
        $cl3 = $instance['slPages_cl3'];
        $cl4 = $instance['slPages_cl4'];
        
    ?>
        <div class="row-fluid footer-link">
            <hr/>
            <div class="row-fluid about-ft">
            <!-- c1 -->
                <div class="span3">
                <?php 
                if(count($cl1) >0):
                foreach ($cl1 as $c): ?>
                      <div><a href="<?php echo get_permalink($c); ?>"><?php echo get_post_field('post_title', $c); ?></a></div>
                <?php
                endforeach;
                endif;
                ?>
                </div>
            <!-- c2 -->
                <div class="span3">
                <?php 
                if(count($cl2) >0):
                foreach ($cl2 as $c): ?>
                      <div><a href="<?php echo get_permalink($c); ?>"><?php echo get_post_field('post_title', $c); ?></a></div>
                <?php
                endforeach;
                endif;
                ?>
                </div>
            <!-- c3 -->
                <div class="span3">
                <?php 
                if(count($cl3) >0):
                foreach ($cl3 as $c): ?>
                      <div><a href="<?php echo get_permalink($c); ?>"><?php echo get_post_field('post_title', $c); ?></a></div>
                <?php
                endforeach;
                endif;
                ?>
                </div>
            <!-- c4 -->
                <div class="span3">
                <?php 
                if(count($cl4) >0):
                foreach ($cl4 as $c): ?>
                      <div><a href="<?php echo get_permalink($c); ?>"><?php echo get_post_field('post_title', $c); ?></a></div>
                <?php
                endforeach;
                endif;
                ?>
                </div>
            </div>
            <hr/>
            <div class="infosx">
                <div class="xlogo">
                    <?php
                            if ( function_exists( 'ot_get_option' ) ) {
                                $bg = ot_get_option('logo_footer');
                                    if($bg && $bg['background-image']){ ?>
                                        <img src="<?php echo $bg['background-image'];?>" alt="Most Coupon"/>
                                   <?php }else{ ?>
                                        <img src="<?php echo get_stylesheet_directory_uri().'/img/logo/footer.png';?>" alt="Most Coupon"/>
                                  <?php }
                                ?>            
                             <?php } 
                         ?>                    
                </div>                
            </div>
        </div>
    <?php
    }
    
    function update($new_instance, $old_instance) { // update widget
        $instance = $old_instance;
        $instance['slPages_cl1'] = $new_instance['slPages_cl1'];
        $instance['slPages_cl2'] = $new_instance['slPages_cl2'];
        $instance['slPages_cl3'] = $new_instance['slPages_cl3'];
        $instance['slPages_cl4'] = $new_instance['slPages_cl4'];
        
        return $instance;
    }
    
    function form($instance) { // form for the widget options
        $slPages_cl1 = $instance['slPages_cl1'];
        $slPages_cl2 = $instance['slPages_cl2'];
        $slPages_cl3 = $instance['slPages_cl3'];
        $slPages_cl4 = $instance['slPages_cl4'];
        
        $args = array(
            'post_type' => 'page',
            'post_status' => 'publish',
            'posts_per_page' => -1
        );
        $all_pages = new WP_Query($args);
        
    ?>
    <div style="color: #333;">
            <p>
                <label><?php echo 'Column 1'; ?></label><br />
                <?php 
                foreach($all_pages->posts as $p): 
                    if(count($slPages_cl1) >0):
                        $checked = '';
                        foreach ($slPages_cl1 as $sl) {
                            if($p->ID == $sl)
                            {
                                $checked = "checked='checked'";
                            }
                        }
                    endif;
                ?>
                <input name="<?php echo $this->get_field_name('slPages_cl1');?>[]" type="checkbox" value="<?php echo $p->ID; ?>" <?php echo $checked; ?> /> <?php echo $p->post_title; ?><br />
                <?php endforeach; ?>
            </p>
            <p>
                <label><?php echo 'Column 2'; ?></label><br />
                <?php 
                foreach($all_pages->posts as $p): 
                    if(count($slPages_cl2) >0):
                        $checked = '';
                        foreach ($slPages_cl2 as $sl) {
                            if($p->ID == $sl)
                            {
                                $checked = "checked='checked'";
                            }
                        }
                    endif;
                ?>
                <input name="<?php echo $this->get_field_name('slPages_cl2');?>[]" type="checkbox" value="<?php echo $p->ID; ?>" <?php echo $checked; ?> /> <?php echo $p->post_title; ?><br />
                <?php endforeach; ?>
            </p>
            <p>
                <label><?php echo 'Column 3'; ?></label><br />
                <?php 
                foreach($all_pages->posts as $p): 
                    if(count($slPages_cl3) >0):
                        $checked = '';
                        foreach ($slPages_cl3 as $sl) {
                            if($p->ID == $sl)
                            {
                                $checked = "checked='checked'";
                            }
                        }
                    endif;
                ?>
                <input name="<?php echo $this->get_field_name('slPages_cl3');?>[]" type="checkbox" value="<?php echo $p->ID; ?>" <?php echo $checked; ?> /> <?php echo $p->post_title; ?><br />
                <?php endforeach; ?>
            </p>
             <p>
                <label><?php echo 'Column 4'; ?></label><br />
                <?php 
                foreach($all_pages->posts as $p): 
                    if(count($slPages_cl4) >0):
                        $checked = '';
                        foreach ($slPages_cl4 as $sl) {
                            if($p->ID == $sl)
                            {
                                $checked = "checked='checked'";
                            }
                        }
                    endif;
                ?>
                <input name="<?php echo $this->get_field_name('slPages_cl4');?>[]" type="checkbox" value="<?php echo $p->ID; ?>" <?php echo $checked; ?> /> <?php echo $p->post_title; ?><br />
                <?php endforeach; ?>
            </p>
    </div>
    <?php    
    }
}
?>
